package org.example.springboot.service;

import org.example.springboot.entity.Order;
import org.example.springboot.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    public List<Order> getOrdersByParentAndTeacher(String name, String teachername, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return orderMapper.findByParentAndTeacher(name, teachername, offset, pageSize);
    }

    public List<Order> getOrdersByParent(String name, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return orderMapper.findByParent(name, offset, pageSize);
    }

    public List<Order> getOrdersByTeacher(String teachername, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return orderMapper.findByTeacher(teachername, offset, pageSize);
    }

    public int countOrdersByParentAndTeacher(String name, String teachername) {
        return orderMapper.countByParentAndTeacher(name, teachername);
    }

    public int countOrdersByParent(String name) {
        return orderMapper.countByParent(name);
    }

    public int countOrdersByTeacher(String teachername) {
        return orderMapper.countByTeacher(teachername);
    }

    public void insertOrder(Order order) {
        orderMapper.createOrder(order);
    }

    public Order getOrderByAll(String user_account, String  teacher_account, String course) {
        return orderMapper.SelectByAll(user_account, teacher_account, course);
    }

    public List<Order> findAllOrders(String teacher_account)
    {
        return orderMapper.findAllOrders(teacher_account);
    }

    public List<Order> findAllOrdersByUser(String user_account)
    {
        return orderMapper.findAllOrdersByUser(user_account);
    }

    @Transactional
    public void updateOrderStatus(int id) {
        orderMapper.updateOrderStatus(id);
    }

    @Transactional
    public void updateOrderDuration(int id) {
        orderMapper.updateOrderDuration(id);
    }

    @Transactional
    public void deleteOrder(int id) {
        orderMapper.deleteOrder(id);
    }

    public Order selectOrder(int id) {
        return orderMapper.selectOrder(id);
    }
}
